package com.mbigger.admin.dao;

import com.mbigger.admin.entity.RoleUser;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface RoleUserMapper {

	@Insert("INSERT INTO sys_role_user (role_id, user_id, operator, operate_time, operate_ip) "
			+ "VALUES (#{roleId}, #{userId}, #{operator}, #{operateTime}, #{operateIp})")
	@Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
	int insert(RoleUser roleUser);
	
	@Delete("DELETE FROM sys_role_user WHERE id=#{id}")
	int deleteById(@Param("id")Integer id);

	@Select("SELECT * FROM sys_role_user WHERE id=#{id}")
	RoleUser selectById(@Param("id")Integer id);
	
	@Update("UPDATE sys_role_user SET role_id=#{roleId}, user_id=#{userId}, operator=#{operator}, operate_time=#{operateTime}, operate_ip=#{operateIp}")
	int updateById(RoleUser roleUser);
	
	@Select("SELECT role_id FROM sys_role_user WHERE user_id=#{userId}")
	List<Integer> getRoleIdListByUserId(@Param("userId")int userId);
	
	@Select("SELECT user_id FROM sys_role_user WHERE role_id=#{roleId}")
	List<Integer> getUserIdListByRoleId(@Param("roleId")int roleId);
	
	@Delete("DELETE FROM sys_role_user WHERE role_id=#{roleId}")
	void deleteByRoleId(@Param("roleId")int roleId);

	@Select("<script>"
			+ "INSERT INTO sys_role_user (role_id, user_id, operator, operate_time, operate_ip) "
			+ "VALUES"
			+ "<foreach collection='roleUserList' item='roleUser' separator=','>"
			+ "(#{roleUser.roleId}, #{roleUser.userId}, #{roleUser.operator}, #{roleUser.operateTime}, #{roleUser.operateIp})"
			+ "</foreach>"
			+ "</script>")
	void batchInsert(@Param("roleUserList")List<RoleUser> roleUserList);
	
	@Select("<script>"
			+ "SELECT user_id FROM "
			+ "WHERE role_id IN "
			+ "<foreach collection='roleIdList' item='roleId' open='(' close=')' separator=','>"
			+ "#{roleId}"
			+ "</foreach>"
			+ "</script>")
	List<Integer> getUserIdListByRoleIdList(@Param("roleIdList")List<Integer> roleIdList);
}